//------------------------------------------------------------------------------
#include <QtGui>
#include <QMessageBox>
#include <QDate>
#include <QSqlRecord>
#include "CFindFrm.h"
#include "CModeReglementFrm.h"
#include "version.h"
#include "CMySQL.h"
#include "CHelp.h"
//------------------------------------------------------------------------------
CModeReglementFrm::CModeReglementFrm(QString name, QString lastCode, QWidget *parent) : CMyMainWindow(name, parent) {
	setActionCur(CGlobal::acConstruct);
	
	setupUi(this);
	
	setStatusBar(NULL);
	
	masterTable="modeReglement";
	primaryKey="code";
	
	loadSchema();
	
	if(CMySQL::getInstance()->getNbEnregistrement(masterTable) == 0) {
		setActionCur(CGlobal::acTableVide);
	}else {
		if(!chargeDonnees(lastCode, CGlobal::seNone)) chargeDonnees("", CGlobal::sePremier);
	}
}
//------------------------------------------------------------------------------
void CModeReglementFrm::setActionCur(CGlobal::EAction action) {
	actionCur=action;
	
	switch(actionCur) {
		case CGlobal::acNone:
			actPremiereFiche->setEnabled(true);
			actFichePrecedente->setEnabled(true);
			actFicheSuivante->setEnabled(true);
			actDerniereFiche->setEnabled(true);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(true);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(true);
			
			enableChamps(true);
			code->setEnabled(false);
			break;
		case CGlobal::acTableVide:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(true);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(false);
			actAnnuler->setEnabled(false);
			
			actRechercheUneFiche->setEnabled(false);
			
			clearChamps();
			enableChamps(false);
			creation->setValue("");
			break;
		case CGlobal::acAjout:
			clearChamps();
			initChamps();
			enableChamps(true);
			code->setFocus();
		case CGlobal::acModif:
			actPremiereFiche->setEnabled(false);
			actFichePrecedente->setEnabled(false);
			actFicheSuivante->setEnabled(false);
			actDerniereFiche->setEnabled(false);
			
			actAjouter->setEnabled(false);
			actSupprimer->setEnabled(false);
			
			actValider->setEnabled(true);
			actAnnuler->setEnabled(true);
			
			actRechercheUneFiche->setEnabled(false);
		default:
			break;
	}
}
//------------------------------------------------------------------------------
CGlobal::EAction CModeReglementFrm::getActionCur(void) {
	return actionCur;
}
//------------------------------------------------------------------------------
void CModeReglementFrm::closeEvent(QCloseEvent * event) {
	if(getActionCur() != CGlobal::acNone && getActionCur() != CGlobal::acTableVide) {
		QMessageBox msgBox(	QMessageBox::Warning, tr("Attention"), 
							tr("Veuillez annuler ou valider avant de sortir"), QMessageBox::Ok);
		msgBox.exec();	
		
		event->ignore();
	}else {
		emit quit(objectName(), curCode);
		event->accept();
	}
}
//------------------------------------------------------------------------------
void CModeReglementFrm::showFormulaire(QSqlQuery *query, CGlobal::EAction& action) {
	QSqlRecord rec=query->record();
	
	curCode=rec.value("code").toString();
	code->setValue(curCode);
	nom->setValue(rec.value("nom"));
	creation->setValue(rec.value("creation"));
}
//------------------------------------------------------------------------------
void CModeReglementFrm::on_actAjouter_triggered(void) {
	setActionCur(CGlobal::acAjout);
	
	creation->setValue(CMySQL::getInstance()->getCurrentDate());
}
//------------------------------------------------------------------------------
void CModeReglementFrm::on_actValider_triggered(void) {
	QString requete;
	QString condition;
	
	if(!validChamps()) return;
	if(!validLiaison()) return;
	
	code->setText(code->text().toUpper());
	switch(getActionCur()) {
		case CGlobal::acAjout:
			requete="INSERT INTO "+masterTable+" SET "+primaryKey+"=\""+code->getValue()+"\", ";
			requete+="creation=CURDATE(), createur=\""+CMySQL::getInstance()->getUser()+"\", ";
			condition="";
			break;
		case CGlobal::acModif:
			requete="UPDATE "+masterTable+" SET ";
			requete+="modification=CURDATE(), modificateur=\""+CMySQL::getInstance()->getUser()+"\", ";
			condition="WHERE "+primaryKey+"=\""+code->getValue()+"\"";
			break;
		default:
			return;
	}
	
	requete+="nom = \""+nom->getValue()+"\" ";
	requete+=condition;
	
	if(CMySQL::getInstance()->execQuery(requete) != 0) {
		curCode=code->text();
		setActionCur(CGlobal::acNone);
	}
}
//------------------------------------------------------------------------------
void CModeReglementFrm::on_actAnnuler_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir annuler ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		if(!chargeDonnees(curCode, CGlobal::seNone)) {
			if(!chargeDonnees(curCode, CGlobal::sePrecedent)) {
				if(!chargeDonnees(curCode, CGlobal::seSuivant)) {
					setActionCur(CGlobal::acTableVide);
					return;
				}
			}
		}
		
		setActionCur(CGlobal::acNone);
	}
}
//------------------------------------------------------------------------------
void CModeReglementFrm::on_actSupprimer_triggered(void) {
	QMessageBox msgBox(	QMessageBox::Question, tr("Confirmation"), 
						tr("Etes vous sûre de vouloir supprimer la fiche courante ?"), QMessageBox::Yes | QMessageBox::No);
	msgBox.exec();
	
	if(msgBox.result() == QMessageBox::Yes) {
		if(!CMySQL::getInstance()->canDelete("client", "modeReglement", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("teteDevis", "modeReglement", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("teteCommandes", "modeReglement", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("teteBLs", "modeReglement", curCode))
			return;
		if(!CMySQL::getInstance()->canDelete("teteFactures", "modeReglement", curCode))
			return;
		
		QString requete="DELETE FROM "+masterTable+" WHERE "+primaryKey+"=\""+curCode+"\"";
		
		CMySQL::getInstance()->execQuery(requete);
		
		if(!chargeDonnees(curCode, CGlobal::sePrecedent)) {
			if(!chargeDonnees(curCode, CGlobal::seSuivant)) {
				setActionCur(CGlobal::acTableVide);
			}
		}
	}
}
//------------------------------------------------------------------------------
void CModeReglementFrm::onChange(void) {
	if(getActionCur() == CGlobal::acNone) {
		setActionCur(CGlobal::acModif);
	}
}
//------------------------------------------------------------------------------
void CModeReglementFrm::on_actRechercheUneFiche_triggered(void) {
	CFindFrm *findFrm=new CFindFrm(masterTable, this);
	
	findFrm->setModal(true);
	findFrm->exec();
	if(findFrm->result() == QDialog::Accepted) {
		chargeDonnees(findFrm->getResult(), CGlobal::seNone);
	}
}
//------------------------------------------------------------------------------
void CModeReglementFrm::onInvokeHelp(CChamp *champ) {
	CHelp::getInstance()->startHelp("setSource qthelp://alpes-ressources.com.coMexpert-"+VERSION_LOGICIEL+"/doc/modeDeReglement.html#"+champ->getTable()+"."+champ->getChamp());
}
//------------------------------------------------------------------------------